package com.artifact.mit.mypoint.activity;

import static com.artifact.mit.mypoint.service.util.Variable.PREFS_NAME;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.service.model.api.User;
import com.artifact.mit.mypoint.service.util.APICall;

public class LoginActivity extends SherlockActivity {

	private Button loginBtn;
	private EditText username;
	private EditText password;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		initActionBar();
		username = (EditText) findViewById(R.id.editText1);
		password = (EditText) findViewById(R.id.editText2);
		loginBtn = (Button) findViewById(R.id.loginBtn);
		loginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(!"".equals(username.getText().toString()) || !"".equals(password.getText().toString())){
					new LoginAsyncTask().execute(username.getText().toString(), password.getText().toString());
				}else{
					Toast.makeText(LoginActivity.this, "Username or Password cannot be empty!", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}

	private class LoginAsyncTask extends AsyncTask<String, Void, User> {
		private ProgressDialog dialog;

		public LoginAsyncTask() {
			dialog = new ProgressDialog(LoginActivity.this);
		}

		protected void onPreExecute() {
			this.dialog.setMessage("loading");
			this.dialog.show();
		}

		@Override
		protected void onPostExecute(final User user) {
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			if (user != null) {
				SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString("id", user.getUserProfile().getId());
				editor.putString("name", user.getUserProfile().getFullName());
				editor.putString("memberUserId", user.getUserProfile().getMemberUserId());
				editor.putString("image", user.getUserProfile().getImage());
				editor.commit();
//				Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			} else {
				Toast.makeText(LoginActivity.this, "Invalid Username or Password", Toast.LENGTH_LONG).show();
			}

		}

		protected User doInBackground(final String... args) {
			User user = APICall.authorize(LoginActivity.this, args[0], args[1]);
			return user;
		}
	}
	
	private void initActionBar() {
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setCustomView(R.layout.actionbar);
	}
}
